/* 初始渐变与图片放大显示 */
const images = Array.from(document.querySelectorAll('.imgmark'));
const popup = document.getElementById('popup');
const popupimg = document.getElementById('popupimg');
const full = document.querySelector('.full');

images.forEach(img => {
    img.addEventListener('click', () => {
        const imgurl = img.getAttribute('data-src');
        popupimg.src = imgurl;
        popup.style.display = 'block';
        full.style.display = 'block';
    });
});

popup.addEventListener('click', () => {
    popup.style.display = 'none';
    full.style.display = 'none';
});

full.addEventListener('click', () => {
    popup.style.display = 'none';
    full.style.display = 'none';
});

const hiddenfull = document.getElementById('fullfadehidden')

hiddenfull.addEventListener('animationend', () => {
    hiddenfull.style.display = 'none';
});

hiddenfull.addEventListener('click', () => {
    hiddenfull.style.display = 'none';
});

/* 右侧导航菜单跟随激活 */
function showContent(divid) {
    var alldiv = document.querySelectorAll('.msgl');
    alldiv.forEach(function(div) {
        if (div.id === divid) {
            div.classList.remove('msghid');
        } else {
            div.classList.add('msghid');
        }
    });

    var indexactive = document.querySelectorAll('.ind');

    indexactive.forEach(function(ind) {
        ind.classList.toggle('indexactive', ind.getAttribute('onclick').includes(divid));
    });

};


// document.addEventListener("DOMContentLoaded", function() {
//     var msgr = document.querySelector('.msg');
//     var msgrOffsetTop = msgr.offsetTop;

//     window.addEventListener('scroll', function() {
//         if (window.pageYOffset > msgrOffsetTop) {
//             msgr.classList.add('fixed');
//         } else {
//             msgr.classList.remove('fixed');
//         }
//     });
// });

// 固定msgr

let msgrfixedaddress;
let msgrfixednow;
let msgrlistnow;
let msgrxynow;

function msgrfixed() {
    var msgrlist = document.getElementById('msgrlist');
    var msgrfixed = document.getElementById('msgrfixed');
    
    var msgrxy = msgrlist.getBoundingClientRect();

    msgrfixed.style.top = msgrxy.top + msgrxynow.height + 10 + 'px';
    msgrfixed.style.left = msgrxy.left + 'px';
}

function dowhi() {
    msgrfixednow = msgrfixedaddress.getBoundingClientRect();
    if (msgrfixednow.top >= 65) {
        msgrfixed();
    } else {
        msgrfixedsave();
    }
}

function msgrfixedsave() {
    var msgrlist = document.getElementById('msgrlist');
    var msgrfixed = document.getElementById('msgrfixed');
    
    var msgrxy = msgrlist.getBoundingClientRect();

    msgrfixed.style.top = 65 + 'px';
    msgrfixed.style.left = msgrxy.left + 'px';
}

function msgrall() {
    msgrfixed();
    msgrfixedaddress = document.getElementById('msgrfixed');
    msgrfixednow = msgrfixedaddress.getBoundingClientRect();
    dowhi();
}

function checkScreenWidth() {
    if (window.innerWidth <= 1500) {
        document.querySelector('.msg').style.display = 'none';
        document.querySelector('.msgr-fixed').style.display = 'none';
        document.querySelector('.msgr').style.display = 'none';
    } else {
        document.querySelector('.msg').style.display = '';
        document.querySelector('.msgr-fixed').style.display = '';
        document.querySelector('.msgr').style.display = '';
    }
}

window.onload = function() {
    msgrfixedaddress = document.getElementById('msgrfixed');
    msgrfixednow = msgrfixedaddress.getBoundingClientRect();
    msgrlistnow = document.getElementById('msgrlist');
    msgrxynow = msgrlistnow.getBoundingClientRect();

    msgrfixed();
    dowhi();
    checkScreenWidth();

    window.addEventListener('resize', dowhi);
    window.addEventListener('resize', checkScreenWidth);
    window.addEventListener('scroll', msgrall);
};

function onPageSwitch() {
    msgrfixed();
}
